<?php
/**
 * Created by PhpStorm.
 * User: Monkey
 * Date: 2021/10/13
 * Time: 11:27
 * Desc: 轮播图
 */
declare (strict_types=1);

namespace app\backend\controller;

use app\backend\base\controller\Base;
use app\backend\validate\BannerValidate;

class Banner extends Base
{
    /**
     * 列表
     */
    public function index()
    {
        if(request()->isPost()){
            $validate = new BannerValidate();
            $ret = $validate->scene('index')->check(input());
            if(!$ret){
                return json(_E($validate->getError()));
            }

            $page = input('page/d',1);
            $limit = input('limit/d',20);

            $list = [];
            $count = \app\extend\model\Banner::count();
            if($count > 0){
                $list = \app\extend\model\Banner::order(['sort'=>'asc','id'=>'asc'])->page($page)->limit($limit)->select();
            }

            return json([
                'code' => 0,
                'msg' => '获取成功',
                'count' => $count,
                'data' => $list,
            ])->send();
        } else {
            return view('index');
        }
    }

    /**
     * 添加
     */
    public function add()
    {
        if(request()->isPost()){
            $validate = new BannerValidate();
            $ret = $validate->scene('add')->check(input());
            if(!$ret){
                return json(_E($validate->getError()));
            }

            $imgUrl = input('img_url','');
            $linkUrl = input('link_url','');
            $sort = input('sort/d',1);

            $result = \app\extend\model\Banner::insert([
                'img_url' => $imgUrl,
                'link_url' => $linkUrl,
                'sort' => $sort,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s')
            ]);
            if(!$result){
                return json(_E('添加失败'));
            } else {
                return json(_S('添加成功'));
            }
        }
    }

    /**
     * 修改
     */
    public function edit()
    {
        if(request()->isPost()){
            $validate = new BannerValidate();
            $ret = $validate->scene('edit')->check(input());
            if(!$ret){
                return json(_E($validate->getError()));
            }

            $id = input('id/d',0);
            $imgUrl = input('img_url','');
            $linkUrl = input('link_url','');
            $sort = input('sort/d',1);

            $result = \app\extend\model\Banner::where([
                ['id','=',$id]
            ])->update([
                'img_url' => $imgUrl,
                'link_url' => $linkUrl,
                'sort' => $sort,
                'updated_at' => date('Y-m-d H:i:s')
            ]);

            if(!$result){
                return json(_E('修改失败'));
            } else {
                return json(_S('修改成功'));
            }
        }
    }

    /**
     * 删除
     */
    public function del()
    {
        if (request()->isPost()) {
            $validate = new BannerValidate();
            $ret = $validate->scene('del')->check(input());
            if(!$ret){
                return json(_E($validate->getError()));
            }
            $id = input('id/d', 0);

            $result = \app\extend\model\Banner::where([
                ['id', '=', $id]
            ])->delete();
            if (!$result) {
                return json(_E('删除失败'));
            } else {
                return json(_S('删除成功'));
            }
        }
    }

    /**
     * 修改状态
     */
    public function editStatus()
    {
        if(request()->isPost()){
            $validate = new BannerValidate();
            $ret = $validate->scene('editStatus')->check(input());
            if(!$ret){
                return json(_E($validate->getError()));
            }

            $id = input('id/d',0);
            $status = input('status/d',1);

            $result = \app\extend\model\Banner::where([
                ['id','=',$id]
            ])->update([
                'status' => $status,
                'updated_at' => date('Y-m-d H:i:s')
            ]);
            if(!$result){
                return json(_E('修改状态失败'));
            } else {
                return json(_S('修改状态成功'));
            }
        }
    }
}
